/* BaseExplorer.java
 * Author: liuex
 * 2007-12-20 ����09:15:14
 *
 * Note: 
 */
package lex.explorer;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.lex.explorer.AbstractExplorer;
import org.lex.utils.logging.PatternFormatter;


public abstract class AbstractExplorer extends JPanel {
	public static final String DIRECTORY_CHANGED_PROPERTY = "directoryChanged";
	public static final String SELECTION_CHANGED_PROPERTY = "selectionChanged";
	// logging
	protected static final Logger log;
	private static final Level level = Level.ALL;
	static {
		PatternFormatter pf = new PatternFormatter("%t [%p] %c{1}.%M(): %m%n");
		ConsoleHandler ch = new ConsoleHandler();
		ch.setFormatter(pf);
		ch.setLevel(level);
		String name = AbstractExplorer.class.getName();
		log = Logger.getLogger(name);
		log.setLevel(level);
		log.addHandler(ch);
		log.setUseParentHandlers(false);
	}
	protected PropertyChangeSupport pcs = null;

	public AbstractExplorer() {
		super();
		this.pcs = new PropertyChangeSupport(this);
	}

	public abstract File getDirectory();

	public abstract void setDirectory(File directory, boolean fireEvent);

	public void setDirectory(File directory) {
		this.setDirectory(directory, true);
	}

	public void refresh() {
		this.setDirectory(this.getDirectory(), false);
	}

	public void addDirectoryChangeListener(PropertyChangeListener l) {
		this.pcs.addPropertyChangeListener(DIRECTORY_CHANGED_PROPERTY, l);
	}

	public void removeDirectoryChangeListener(PropertyChangeListener l) {
		this.pcs.removePropertyChangeListener(DIRECTORY_CHANGED_PROPERTY, l);
	}

	protected void fireDirectoryChange(final File old, final File now) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				pcs.firePropertyChange(DIRECTORY_CHANGED_PROPERTY, old, now);
			}
		});
	}
}
